﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class EditFrom : Form
    {
        private int id;
        private string title;
        private string author;
        private string createdTime;

        public EditFrom()
        {
            InitializeComponent();
        }

        public EditFrom(int id, string title, string author, string createdTime)
        {
            this.id = id;
            this.title = title;
            this.author = author;
            this.createdTime = createdTime;
        }

        private void EditFrom_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var author = textBox2.Text;

            //更新
            if (id > 0)
            {

                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("update Articles set Title='{0}', Author = '{1}',CreatedTime = '{2}' where Id='{3}'", title, author, createdTime, id);
                    var res = DebHelper.AddOrUpdateOrDelect(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！");
                    }
                }
                else
                {
                    MessageBox.Show("需要修改！");
                }
            }
            //添加
            else
            {

                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Articles(Title,Author,CreatedTime) values('{0}','{1}','{2}')", title, author, createdTime);
                    var res = DebHelper.AddOrUpdateOrDelect(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("标题不能为空！");
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
